package com.sskj.fabi.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.PublishRecordBean;
import com.sskj.fabi.presenter.PublishRecordFragmentPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.FabiBuySellEnum;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.FABI_FRAGMENT_PUBLISH_RECORD)
public class PublishRecordFragment extends BaseFragment<PublishRecordFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Autowired
    boolean isBuy;
    private MVCCoolHelper<List<PublishRecordBean>> mvcHelper;
    private int curPos;
    private SlimAdapter slimAdapter;
    private MaterialDialog cancelTip;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_publish_record;
    }

    @Override
    public PublishRecordFragmentPresenter getPresenter() {
        return new PublishRecordFragmentPresenter();
    }


    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.fabi_publishRecordFragment1_h));
        statusMap.put("2", App.INSTANCE.getString(R.string.fabi_fabiOrderStatusEnum3_h));
        statusMap.put("3", App.INSTANCE.getString(R.string.fabi_publishRecordFragment3_h));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.fabi_recy_item_publish_record, new SlimInjector<PublishRecordBean>() {
            @Override
            public void onInject(PublishRecordBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tv1, String.format(App.INSTANCE.getString(R.string.fabi_publishRecordFragment4_h), isBuy ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle()))
                        .text(R.id.tv2, String.format(App.INSTANCE.getString(R.string.fabi_publishRecordFragment5_h), isBuy ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle()))
                        .text(R.id.tv3, String.format(App.INSTANCE.getString(R.string.fabi_publishRecordFragment6_h), isBuy ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle()))
                        .text(R.id.tv_order_date, data.getAdd_time())
                        .text(R.id.tv_order_price, "￥" + CoinUtil.keepRMB(data.getPrice()))
                        .text(R.id.tv_order_amount, CoinUtil.keepUSDT(data.getTrans_num()) + " USDT")
                        .text(R.id.tv_deal_money, "￥" + CoinUtil.keepRMB(data.getTotalprice()))
                        .text(R.id.tv_limit_small, "￥" + CoinUtil.keepRMB(data.getMin_price()))
                        .text(R.id.tv_limit_big, "￥" + CoinUtil.keepRMB(data.getMax_price()))
                        .visibility(R.id.iv_wechat, "1".equals(data.getPay_wx()) ? View.VISIBLE : View.GONE)
                        .visibility(R.id.iv_bank, "1".equals(data.getPay_backcard()) ? View.VISIBLE : View.GONE)
                        .visibility(R.id.iv_alipay, "1".equals(data.getPay_alipay()) ? View.VISIBLE : View.GONE)
                        .clicked(R.id.tv_order_status, view -> {
                            if ("1".equals(data.getStatus())) {
                                curPos = slimAdapter.getData().indexOf(data);
                                cancelOrder();
                            }
                        })
                        .text(R.id.tv_order_status, statusMap.get(data.getStatus()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<PublishRecordBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<PublishRecordBean>)
                page -> mPresenter.getData(page + "", "20", isBuy), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    private void cancelOrder() {
        if (cancelTip == null) {
            cancelTip = TipUtil.getSureCancelTip(getActivity(), App.INSTANCE.getString(R.string.fabi_publishRecordFragment7_h), () -> {
                cancelTip.dismiss();
                PublishRecordBean dataItem = (PublishRecordBean) slimAdapter.getDataItem(curPos);
                mPresenter.cancelPublishOrder(isBuy, dataItem.getId());
            });
        } else {
            cancelTip.show();
        }

    }

    public void cancelSuccess() {
        mvcHelper.refresh();
    }
}
